//
//  FourViewController.m
//  项目二
//
//  Created by rimi on 16/5/13.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "FourViewController.h"

@interface FourViewController ()

/**UIImageView*/
@property(nonatomic,strong)UIImageView *ballView;
/**UIView*/
@property(nonatomic,strong)UIView *animationView;

@end

@implementation FourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手势、动画、触摸";
    self.view.backgroundColor = [UIColor whiteColor];
    [self ballImage];
    [self addGestureToAnimationView];
}

-(void)ballImage{
    _ballView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball副本"]];
    _ballView.frame = CRM(100, 100, 50, 50) ;
    [self.view addSubview:_ballView];
}

/**手势*/
- (void)addGestureToAnimationView {
    
    _animationView = [[UIView alloc]init];
    _animationView.bounds = CRM(0, 0, 200, 200);
    _animationView.center = self.view.center;
    _animationView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:_animationView];
 
    /**如果是 imageView 需要把用户交互打开*/
    _animationView.userInteractionEnabled = YES;
    
    /**单击手势*/
    /**1.初始化手势控制器*/
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAnimationView:)];
    /**2.为视图添加手势*/
    [_animationView addGestureRecognizer:tapGes];
    
    /**缩放手势*/
    UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAnimationView:)];
    [_animationView addGestureRecognizer:pinchGes];
    
    /**拖动手势*/
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAnimationView:)];
    [_animationView addGestureRecognizer:panGes];
}
#pragma mark --- method
/**单击手势事件*/
- (void)tapAnimationView:(UITapGestureRecognizer *)sender{
   [UIView animateWithDuration:0.5 animations:^{
       _animationView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) + 100);
       _animationView.backgroundColor = [UIColor greenColor];
   } completion:^(BOOL finished) {
       _animationView.backgroundColor = [UIColor blackColor];
   }];
    
    /**位移、背景颜色的改变*/
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        /**在动画快里改变视图的可见属性*/
        _animationView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidX(self.view.bounds) + 200);
    } completion:^(BOOL finished) {
        _animationView.backgroundColor = [UIColor blackColor];
    }];
     /**缩放、旋转*/
    [UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
        _animationView.bounds = CRM(0, 0, 100, 100);
        /**旋转*/
        _animationView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } completion:^(BOOL finished) {
        /**翻转*/
       [UIView transitionWithView:_animationView duration:3 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationTransitionFlipFromLeft animations:nil completion:^(BOOL finished) {
           _animationView.backgroundColor = [UIColor blackColor];
       }];
    }];
}
/**缩放手势*/
-(void)pinchAnimationView:(UIPinchGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"手势开始了");
    }else if (sender.state == UIGestureRecognizerStateChanged){
        NSLog(@"手势改变了");
        /** CGAffineTransformScale()变换效果是叠加的*/
        /**当手势改变时执行缩放操作*/
        sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
        /**保存缩放系数为1*/
        sender.scale = 1;
    }else if (sender.state == UIGestureRecognizerStateEnded){
        NSLog(@"手势结束了");
        /**取消一切形变*/
        sender.view.transform = CGAffineTransformIdentity;
    }
   
}
/**拖动手势*/
- (void)panAnimationView:(UIPanGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateChanged) {
        /**获取手指在指定视图  self.view的移动坐标*/
        CGPoint transition = [sender translationInView:self.view];
        NSLog(@"%@",NSStringFromCGPoint(transition));
        /**更新_animationView的center*/
        sender.view.center = CGPointMake(sender.view.center.x + transition.x, sender.view.center.y + transition.y);
        /**把增量置为0*/
        [sender setTranslation:CGPointZero inView:self.view];
    }else if (sender.state == UIGestureRecognizerStateEnded){
    
    }
}

#pragma mark -- 触摸事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    /**获得touch对象*/
    UITouch *touch = touches.anyObject;
    /**通过touch的对象获得当前点击位置的坐标*/
    CGPoint location = [touch locationInView:self.view];
    /**弹性动画
     usingSpringWithDamping: 阻尼系数，0 - 1，阻尼系数越接近0，弹簧效果越明显。
     initialSpringVelocity: 数值越大，初始速度越快*/
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:0 animations:^{
        /**更新球的坐标*/
        _ballView.center = location;
        /**更新大小*/
        _ballView.bounds = CRM(0, 0, 400, 400);
    } completion:^(BOOL finished) {
        _ballView.bounds = CRM(0, 0, 50, 50);
    }];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    NSLog(@"点击结束");
}
@end
